package cn.com.oceansoft.business.tcsb.controller;


import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.tcsb.util.PicThread;
import cn.com.oceansoft.business.tcsb.entity.TcsbXx;
import cn.com.oceansoft.business.tcsb.service.ITcsbXxService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * Created by lenovo on 2018/12/11.
 */
@Controller
@RequestMapping("api/tcsb")
public class ApiTcsbController {
    @Resource
    private ITcsbXxService tcsbXxService;

    @RequestMapping(value = "tcsb")
    public String gotoMattersPageWithUser( HttpServletRequest request, Model model) throws IllegalArgumentException, IOException {
        System.out.println("==openid==="+(String) request.getSession().getAttribute("openId"));
      return "mobile/tcsb/tcsb";
    }
    /**
     * 信息申报
     * @param request
     * @param tcsbXx
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "submit", method = RequestMethod.POST)
    public Result submit(HttpServletRequest request, TcsbXx tcsbXx) {
        tcsbXx.setOpenid((String) request.getSession().getAttribute("openId"));
        Result result=new Result();
        try{
            tcsbXxService.saveTcsbXx(tcsbXx);
            result.setData(tcsbXx.getId());
            result.setSucc(true);
            result.setMsg("申报成功");
            PicThread thread=new PicThread(tcsbXx,tcsbXxService);
            thread.start();
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        result.setSucc(false);
        result.setMsg("申报失败");
        return result;
    }
    @ResponseBody
    @RequestMapping(value = "getList", method = RequestMethod.POST)
    public Result getList(HttpServletRequest request, Query query) {
        query.setNumPerPage(10);
        Map<String, String> map = new HashMap<>();
        map.put("openid",(String)request.getSession().getAttribute("openId"));
        query.setMap(map);
        Result result=new Result();
        Pager<TcsbXx> list=tcsbXxService.getXxList(query);
        result.setSucc(true);
        result.setMsg("查询成功");
        result.setData(list);
        return result;
    }

    @RequestMapping(value = "detail")
    public String detail( HttpServletRequest request, Model model) throws IllegalArgumentException, IOException {
        String id=request.getParameter("id");
        model.addAttribute("id",id);
        return "mobile/tcsb/tcsb-xq";
    }
    @ResponseBody
    @RequestMapping(value = "getDetailOne", method = RequestMethod.POST)
    public Result getDetailOne(HttpServletRequest request) {
        String id=request.getParameter("id");
        TcsbXx tcsbXx=tcsbXxService.getOneXx(id);
        List<Map<String,Object>>  pic=tcsbXxService.getPhotoByID(id);
        JSONObject object=new JSONObject();
        object.put("content",tcsbXx);
        object.put("pic",pic);
        Result result=new Result();
        result.setSucc(true);
        result.setMsg("查询成功");
        result.setData(object);
        return result;
    }
}
